/**
 *
 */
package cn.dw.mm.controller;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.nullValue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.dw.mm.domain.User;
import cn.dw.mm.service.UserService;

/**
 * @author aubrey
 * @date  上午9:29:14
 * 
 */
@Controller
public class LoginController {
	
	@Autowired
	private UserService userService;
	
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public String login(@RequestBody User user, HttpServletRequest req) {
		String id_str = userService.login(user);
		if (id_str != null) {
			Integer id = Integer.parseInt(id_str);
			if(id > 0) {
				HttpSession session = req.getSession();
				session.setAttribute("user", id);
				return id_str;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	@RequestMapping(value = "/logout", method = RequestMethod.POST)
	@ResponseBody
	public Boolean logout(HttpServletRequest req) {
		HttpSession session = req.getSession();
		session.removeAttribute("user");
		return true;
	}
	
	
}
